<template>
	<div v-if="contentLoaded" style="paddingBottom: 3rem">
		<router-link to="/address">
			<AddressHeader :address="addressInfo"></AddressHeader>
		</router-link>
		<PayList></PayList>
		<PayNote :bonus="bonus" :postage="postage" ph="可以在这留下您的备注…" v-model="note"></PayNote>
		<PayFooter :price="totalPrice" @pay="pay" :class="{disabled: !available }"></PayFooter>
	</div>
</template>
<script>
import { Indicator, Toast} from 'mint-ui'
import { mapMutations, mapGetters, mapActions } from 'vuex'
import { handleHistory } from '@/assets/js/utils'
import AddressHeader from '@/components/AddressHeader'
import PayList from './PayList'
import PayNote from './PayNote'
import PayFooter from './PayFooter'
import WxLogin from '@/api/wxlogin'
export default{
	name:'CreateOrder',
	async created(){
		// await this.WxPayLogin()
		await this.settleCartGoods()
		await this.wxconfigStart();
		// handleHistory(this.$router, '/')
	},
	data(){
		return {
			contentLoaded: false,
            available: true,
            note: ''
		}
	},
	computed:{
		...mapGetters({
			addressInfo: 'pay/addressInfo',
			goodsInfo: 'pay/goodsInfo',
			postage: 'pay/postage',
			bonus: 'pay/bonus',
			totalPrice: 'pay/totalPrice'
		}),
	},
	methods:{
		...mapActions({
			toSettleCartGoods: 'pay/toSettleCartGoods',
			submitOrder: 'pay/submitOrder',
			payOrder: 'pay/payOrder',
		}),
		async WxPayLogin() {
			Indicator.open('支付环境监测中…')
			// await WxLogin.ifWxPayLoggedIn()
			Indicator.close()
		},
		async settleCartGoods() {
			Indicator.open()
			this.available = await this.toSettleCartGoods()
			// 若无待结算商品 则返回首页
			if(!this.available) this.$router.push('')
			this.contentLoaded = true
			Indicator.close()
		},
		async pay() {
            if(!this.addressInfo) Toast('请填写地址')
			if(!this.available || !this.addressInfo) return
			Indicator.open()
			let types = window.localStorage['deductible'];
			console.log('下单的type',types);
			const orderFormLogId = await this.submitOrder({note: this.note,type:types})
			Indicator.close()
			this.payPrice(orderFormLogId);
			// this.payOrder({router: this.$router, orderFormLogId: orderFormLogId, type: 1})
		},
		wxconfigStart(){//微信sdk初始化
			let url = window.location.href.split('#')[0];
			console.log('url',url)
			this.$http.post('http://we.hzchuangxiangzhe.cn/wukong/php/PcApi',{name:'shopping.sys.WXpay.getWxSign',url:url},{emulateJSON:true}).then((res)=>{
				wx.config({
					debug:false,
					appId:'wxca54e54a9155264a',
					timestamp:res.body.data.timestamp,
					nonceStr: res.body.data.nonceStr,
					signature: res.body.data.signature,
					jsApiList:['chooseWXPay']
				})
			}).catch((error)=>{
				console.log(error);
			})
		},            
		payPrice(order){//唤起微信支付
			this.$http.post('http://we.hzchuangxiangzhe.cn/wukong/php/PcApi',{name:'shopping.sys.WXpay.normalGoods',order_id:order},{emulateJSON:true}).then((res)=>{
				if(res.body.code === 1000){
					alert(JSON.stringify(res));
					let datas = res.body.data.pay_need;
					wx.chooseWXPay({
						timestamp:datas.timestamp_pay,
						nonceStr:datas.nonceStr_pay,
						package:"prepay_id="+datas.package,
						signType:datas.signType,
						paySign:datas.sign_pay,
						success:(res)=>{
							console.log(JSON.stringify(res));
						},
						cancel:(res)=>{
							console.log(JSON.stringify(res));
						},
						fail:(err)=>{
							console.log(JSON.stringify(err));
						}
					})
				}else{
					alert(JSON.stringify(res));
				}
			}).catch((error)=>{
				console.log(error);
			})
		},
    },
	components: { AddressHeader, PayList, PayNote, PayFooter }
}
</script>
<style lang='less' scoped></style>
